<?php

class OrderController extends \Base\AbstractController {

    /**
     * @desc 列表
     */
    public function listAction() {
        $request = $this->getRequest();
        $type    = $request->get('type');
        $orderId = (int)$request->get('order_id');
        $status  = $request->get('status');
        $where   = [];

        if($type == 'buy') {
            $where['type'] = 1;  //看大腿
        } elseif($type == 'sell') {
            $where['type'] = 2;  //求大腿
        }

        if($orderId > 0) {
            $where['id'] = $orderId;
        }

        if(!empty($status)) {
            $where['status'] = $status;
        }

        $page    = new \Lib\Paginator('order');
        $page->where($where);
        $this->assign('count', $page->count());
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $memberMapper = \Mapper\MemberModel::getInstance();
        $models       = $page->lists();
        $return       = [];

        foreach($models as $model) {
            $data       = $model->toArray();
            $fromMember = $memberMapper->findById($data['from_uid']);
            $toMember   = $memberMapper->findById($data['to_uid']);

            if($fromMember instanceof MemberModel) {
                $data['from_nick_name'] = urldecode($fromMember->getNick_name());
            }

            if($toMember instanceof MemberModel) {
                $data['to_nick_name'] = urldecode($toMember->getNick_name());
            }

            $return[] = $data;
        }

        $this->assign('lists', $return);
    }


    /**
     * @desc 详细页
     */
    public function editAction() {
        $request = $this->getRequest();
        $id      = $request->get('id');
        $return  = [];

        $orderMapper  = \Mapper\OrderModel::getInstance();
        $memberMapper = \Mapper\MemberModel::getInstance();
        $gameMapper = \Mapper\SparringGameModel::getInstance();
        $detailMapper = \Mapper\SparringBuyDetailModel::getInstance();
        $data         = $orderMapper->findById($id)->toArray();

        $data['from_nick_name'] = urldecode($memberMapper->findById($data['from_uid'])->getNick_name());
        $data['to_nick_name']   = urldecode($memberMapper->findById($data['to_uid'])->getNick_name());

        $sparringNos  = explode(',', $data['sparring_no']);
        $detailModels = $detailMapper->fetchAll(['id' => $sparringNos]);

        foreach($detailModels as $detailModel) {
            $detailData              = $detailModel->toArray();
            $detailData['game_name'] = $gameMapper->findById($detailData['game_id'])->getGame_name();

            $return[] = $detailData;
        }

        $this->assign('detail',$return);
        $this->assign('data', $data);
        $this->display('edit');
        return false;
    }

}
